package oa.hleast.xswl.service.questionnaire.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.xswl.mapper.questionnaire.QmsProblemMapper;
import oa.hleast.xswl.pojo.domain.questionnaire.QmsAnswer;
import oa.hleast.xswl.pojo.domain.questionnaire.QmsProblem;
import oa.hleast.xswl.pojo.domain.questionnaire.QmsQuestionnaire;
import oa.hleast.xswl.pojo.vo.ShowQuestionnaireVo;
import oa.hleast.xswl.pojo.vo.questionnaire.QmsAnswerVo;
import oa.hleast.xswl.pojo.vo.questionnaire.QmsProblemVo;
import oa.hleast.xswl.service.ISysCommonConfigService;
import oa.hleast.xswl.service.questionnaire.QmsAnswerService;
import oa.hleast.xswl.service.questionnaire.QmsProblemService;
import oa.hleast.xswl.service.questionnaire.QmsQuestionnaireService;
import oa.hleast.xswl.service.questionnaire.QuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author: 302
 * @date: 2021-07-19 18:43:06
 * @description: 问卷管理接口实现
 */
@Slf4j
@Transactional
@Service("questionnaireService")
@AllArgsConstructor
public class QuestionnaireServiceImpl implements QuestionnaireService {
    private final QmsQuestionnaireService qmsQuestionnaireService;
    private final QmsAnswerService qmsAnswerService;
    private final QmsProblemService qmsProblemService;
    private final ISysCommonConfigService commonConfigService;

    @Autowired
    private QmsProblemMapper qmsProblemMapper;

    /**
     * 获取压力测试问卷数据
     */
    @Override
    public ShowQuestionnaireVo getAntiStressQuestionnaire(Long questionnaireId) {
        ShowQuestionnaireVo showQuestionnaireVo = new ShowQuestionnaireVo();

        List<QmsProblem> qmsProblemList = qmsProblemService.list(new LambdaQueryWrapper<QmsProblem>().eq(QmsProblem::getQuestionnaireId, questionnaireId));
        if(qmsProblemList==null){
            showQuestionnaireVo.setExist(false);
        }else {
            showQuestionnaireVo.setExist(true);
        }

        // 从通用配置中获取压力测试的 主键ID
        // 获取问卷数据
        QmsQuestionnaire questionnaire = qmsQuestionnaireService.getById(questionnaireId);

        // 获取试题数据
        List<QmsProblem> list = qmsProblemService.list(new LambdaQueryWrapper<QmsProblem>()
                .eq(QmsProblem::getQuestionnaireId, questionnaireId)
                .orderByAsc(QmsProblem::getProblemNumber)
                .orderByDesc(QmsProblem::getGmtModified)
                .orderByDesc(QmsProblem::getGmtCreate)
        );

        if(list==null){
            showQuestionnaireVo.setExist(false);
        }else {
            showQuestionnaireVo.setExist(true);
        }

        List<QmsProblemVo> problemVoList =  list.stream().map(pojo ->{
            // 获取答案数据
            List<QmsAnswer> qmsAnswerList = qmsAnswerService.list(new LambdaQueryWrapper<QmsAnswer>().eq(QmsAnswer::getProblemId, pojo.getId()));
            List<QmsAnswerVo>  answerVoList = qmsAnswerList.stream().map(answer ->{
                QmsAnswerVo answerVo = new QmsAnswerVo();
                BeanUtil.copyProperties(answer,answerVo);
                return answerVo;
            }).collect(Collectors.toList());

            QmsProblemVo qmsProblemVo = new QmsProblemVo();
            BeanUtil.copyProperties(pojo,qmsProblemVo);
            qmsProblemVo.setQmsAnswerVoList(answerVoList);

            return qmsProblemVo;
        }).collect(Collectors.toList());

        int size = problemVoList.size();
        List<QmsProblemVo> qmsProblemVoList=new ArrayList<>();
        while (qmsProblemVoList.size()<questionnaire.getTitleNumber()){
            Random random=new Random();
            int index=random.nextInt(size);
            qmsProblemVoList.add(problemVoList.get(index));
            problemVoList.remove(index);
            size--;
        }


        BeanUtil.copyProperties(questionnaire,showQuestionnaireVo);
        showQuestionnaireVo.setProblemVoList(qmsProblemVoList);
        // 获取答案数据
        return showQuestionnaireVo;
    }


}
